<template>
    <div>
        <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="125px" :disabled="disabled">
            <el-row :gutter="15">
                <el-col :span="12">
                    <el-form-item label="用印单位" prop="dept">
                        <dept-select-tree v-model="formData.dept" placeholder="请选择用印单位" clearable :style="{width: '100%'}" dept-type="my" :disabled="disabled"></dept-select-tree>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="申请人" prop="alias">
                        <el-input v-model="formData.alias" placeholder="请输入申请人" clearable :style="{width: '100%'}"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item label="印章类型" prop="type_id">
                <el-radio-group v-model="formData.type_id" @change="changeTypeId">
                    <el-radio :label="1">党政印章</el-radio>
                    <el-radio :label="2">合同用印</el-radio>
                </el-radio-group>
            </el-form-item>
            <template v-if="formData.type_id==1">
                <el-form-item label="用印事项" prop="title">
                    <el-input type="textarea" v-model="formData.title" placeholder="请输入用印事项" clearable :style="{width: '100%'}" :disabled="disabled" :autosize="{minRows: 1, maxRows: 4}">
                    </el-input>
                </el-form-item>
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="中共湘潭大学委员会印章数量" prop="num1"  class="two-line">
                            <el-input-number :min="0" v-model="formData.num1" placeholder="请输入中共湘潭大学委员会印章数量" clearable :style="{width: '100%'}" @input="setNum">
                            </el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="湘潭大学印章数量" prop="num2">
                            <el-input-number :min="0" v-model="formData.num2" placeholder="请输入湘潭大学印章数量" clearable :style="{width: '100%'}" @input="setNum">
                            </el-input-number>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="湘潭大学钢印数量" prop="num3">
                            <el-input-number :min="0" v-model="formData.num3" placeholder="请输入湘潭大学钢印数量" clearable :style="{width: '100%'}" @input="setNum">
                            </el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="校长身份证、签字章数量" prop="num4" class="two-line">
                            <el-input-number :min="0" v-model="formData.num4" placeholder="请输入校长身份证、签字章数量" clearable :style="{width: '100%'}" @input="setNum">
                            </el-input-number>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="书记身份证、签字章数量" prop="num5" class="two-line">
                            <el-input-number :min="0" v-model="formData.num5" placeholder="请输入书记身份证、签字章数量" clearable :style="{width: '100%'}" @input="setNum">
                            </el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="事业单位法人证书（副本）复印件" prop="num6" class="two-line">
                            <el-input-number :min="0" v-model="formData.num6" placeholder="请输入事业单位法人证书（副本）复印件" clearable :style="{width: '100%'}" @input="setNum">
                            </el-input-number>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="数量" prop="num">
                            <el-input-number :min="0" v-model="formData.num" placeholder="请输入数量" clearable :style="{width: '100%'}" :disabled="true"></el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                    </el-col>
                </el-row>
            </template>
            <template v-else>
                <el-form-item label="合同名称" prop="title">
                    <el-input type="textarea" v-model="formData.title" placeholder="请输入合同名称" clearable :style="{width: '100%'}" :disabled="disabled" :autosize="{minRows: 1, maxRows: 4}">
                    </el-input>
                </el-form-item>
                <el-form-item label="金额区间" prop="jiner">
                    <el-radio-group v-model="formData.jiner">
                        <el-radio :label="0">0-10万元</el-radio>
                        <el-radio :label="1">10-50万元</el-radio>
                        <el-radio :label="2">50万以上</el-radio>
                    </el-radio-group>
                </el-form-item>
            </template>
            <el-form-item label="备注">
                <el-input v-model="formData.remark" type="textarea" placeholder="请输入备注" clearable :style="{width: '100%'}" :autosize="{minRows: 3, maxRows: 4}"></el-input>
            </el-form-item>
            <el-form-item label="上传附件">
                <my-upload-file v-model="formData.files" :disabled="disabled"></my-upload-file>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
    import DeptSelectTree from "@/components/Common/DeptSelectTree";
    import MyUploadFile from "../../../components/Common/MyUploadFile";
    import {mapGetters} from "vuex";

    export default {
        name: "LeftForm",
        inheritAttrs: false,
        components: {MyUploadFile, DeptSelectTree},
        props: ["row", "disabled", "isNew", "isUpdate"],
        data() {
            return {
                formData: {
                    title: undefined,
                    dept: undefined,
                    alias: undefined,
                    type_id: 1,
                    remark: undefined,
                    jiner: 0,
                    num: undefined,
                    num1: undefined,
                    num2: undefined,
                    num3: undefined,
                    num4: undefined,
                    num5: undefined,
                    files: undefined,
                    num6:undefined
                },
                rules: {
                    title: [{
                        required: true,
                        message: '请输入用印事项',
                        trigger: 'blur'
                    }],
                    dept: [{
                        required: true,
                        message: '请选择用印单位',
                        trigger: 'change'
                    }],
                    date: [{
                        required: true,
                        message: '请选择用印时间',
                        trigger: 'change'
                    }],
                    type_id: [{
                        required: true,
                        message: '请选择印章归属部门',
                        trigger: 'change'
                    }],
                    num: [{
                        required: true,
                        pattern: /^[1-9]\d*$/,
                        message: '至少需要输入一个印章数量',
                        trigger: 'change'
                    }],
                    remark: [],
                },
            }
        },
        computed: {
          ...mapGetters(["userinfo"]),
            type_id() {
                let path = this.$route.fullPath.split("/")
                let key = path[path.length - 1]
                return key == "party" ? 1 : 0
            },
            labelTitle() {
                return this.type_id == 1 ? "校领导" : "领导"
            },
        },
        watch: {
            "formData.type_id"(v) {
                if (v == 1) {
                    this.formData.num1 = undefined
                    this.formData.num2 = undefined
                    this.formData.num3 = undefined
                    this.formData.num4 = undefined
                    this.formData.num5 = undefined
                    this.formData.num6 = undefined
                    if (!this.formData.id) this.formData.num = undefined
                }
                this.$refs['elForm'].clearValidate("title")
            },
        },
        created() {
              this.formData.alias = this.userinfo.alias;
            this.initData()
        },
        mounted() {
        },
        methods: {
            changeTypeId() {
                if (this.formData.type_id == 1) {
                    this.rules.title = [{
                        required: true,
                        message: '请输入用印事项',
                        trigger: 'blur'
                    }]
                } else {
                    this.rules.title = [{
                        required: true,
                        message: '请输入合同名称',
                        trigger: 'blur'
                    }]
                }
            },
            setNum() {
                let num1 = this.formData.num1 ? this.formData.num1 : 0
                let num2 = this.formData.num2 ? this.formData.num2 : 0
                let num3 = this.formData.num3 ? this.formData.num3 : 0
                let num4 = this.formData.num4 ? this.formData.num4 : 0
                let num5 = this.formData.num5 ? this.formData.num5 : 0
                let num6 = this.formData.num6 ? this.formData.num6 : 0
                this.$set(this.formData, "num", num1 + num2 + num3 + num4 + num5+ num6)
            },
            initData() {
                for (let o in this.row) {
                    if (this.row[o] != undefined) {
                        this.formData[o] = this.row[o]
                    }
                }
            },
          submitForm() {
                let self = this
                return new Promise(function (resolve, reject) {
                    self.$refs['elForm'].validate(valid => {
                        if (!valid) return
                        self.setNum()
                        resolve(self.formData)
                    })
                });
            },
            resetForm() {
                this.$refs['elForm'].resetFields()
            },
        }
    }

</script>
<style lang="scss" scoped>
    ::v-deep .two-line {
        .el-form-item__label {
            line-height: 20px;
        }
    }
    .line {
        text-align: center;
        line-height: 32px;
    }
</style>

